var socketTool = require("./socketTool.js").socketTool
//socket操作类
var Socket = {}
//监听连接操作
Socket._connection = (namespace,cb) => {
    namespaceName = io.of("/" + namespace);
    console.log(namespace + "连接成功")
    namespaceName.on('connection',(socket)=>{
        console.log(socket.id + "连接成功")
        cb(socket)

    });
}

//验证注册
Socket._register = (socket) => {

    socket.on("register",(data) => {

        //进行token的验证
        socketTool.vToken(data,(rs,errorType,text)=>{

            if(!rs){
                this.Socket._return(rs,errorType,text,socket)
                return
            }

            //进行全局存储
            socketTool.vSocketUser(data,socket,()=>{

                this.Socket._return(true,200,"请求成功",socket)

            })

        })

    })
}
//返回操作
Socket._return = (isture,errorType,errorText,socket) => {
    //200 正确，404 退出连接，409 一般性错误，
    var result = {}
    result.success = isture
    result.result  = {}
    result.result.code  = errorType?errorType:200
    result.result.data  =errorText?errorText:"请求成功"
    // console.log(result)

    socket.emit("Return",result)
    return;
}
//监听退出操作
Socket._disconnect = (socket) => {
    socket.on('connecting', function(){
        console.log('正在连接...')
    })
}

//发送单聊消息
Socket._privateChat = (toData,to_id,cb) => {
    //toData.type -0普通系统消息
    //            -1群销毁或新建消息
    //            -2有新消息

    var toSocket  = socketTool.vObtainSocketUser(to_id)

    // console.log(toSocket == false)
    if(toSocket == false){
        cb(false)
    }else{

        if(toData.messageType == 'systemMessage'){
            //发送系统消息

            toSocket.emit('systemMessage',toData);
            cb(true)
        }else if(toData.messageType == 'singleChat'){
            //发送私聊信息
            toSocket.emit('sayToYou',toData);
            cb(true)
        }

    }

}
//刷新用户信息
Socket._refreshUserInformation = (data,cb) => {
    socketTool.vSocketUser(data,"",() => {})
    cb(true)
}
//检查用户在线状态
Socket._checkingUserStatus = (userId,cb) => {
    var vSocket = socketTool.vObtainSocketUser(userId)
    cb(vSocket != false)
}
//创建房间
Socket._createRoom = (roomData,cb) => {
    var vChatRoom = socketTool.vCreateRoom(roomData)
    cb(vChatRoom != false)
}
//销毁房间
Socket._destroyRoom = (roomData,cb) => {
    //给房间内所有人发送系统消息
    var key = 'R_' + roomData.id
    this.Socket._sendMessagesToRoomStaff(key,"房间已销毁")

    var vChatRoom = socketTool.vDestroyRoom(roomData)
    cb(vChatRoom != false)
}
//用户加入房间
Socket._joinRoom = (datas,cb) => {
    var key = socketTool.vJoinRoom(datas)
    this.Socket._sendMessagesToRoomStaff(key,datas.nickname + "加入了房间")
    cb(true)
}
//用户退出房间
Socket._signOutRoom = (datas,cb) => {
    var key = socketTool.vSignOutRoom(datas)
    this.Socket._sendMessagesToRoomStaff(key,datas.userName + "退出了房间")
    cb(true)
}
//给房间内的用户发送消息
Socket._sendMessagesToRoomStaff = (roomID,data) => {
    namespaceName.to(roomID).emit("jionRoom",data)
}


exports.Socket = Socket